<?php

/**
 * schedule actions.
 *
 * @package    DUYTAN
 * @subpackage schedule
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class scheduleActions extends sfActions
{
 /**
  * Executes index action
  *
  * @param sfRequest $request A request object
  */
  public function executeIndex(sfWebRequest $request)
  {
  	$this->schedule_form = new DtScheduleForm();
	$form_data = $request->getParameter('schedule');
	if ($request->hasParameter('create') && $request->getParameter('create') == 'Create') {
		$this->create_success = false;
		if (!empty($form_data['ground_id']) && !empty($form_data['schedule']) && !empty($form_data['cost'])) {
			$ground_id = $form_data['ground_id'];
			$schedule_name = $form_data['schedule'];
			$cost = $form_data['cost'];
			$cost_weekend = $form_data['cost_weekend'];
			$promotion = $form_data['promotion'];
			$schedule = Doctrine_Core::getTable('DtSchedule')->find(array($ground_id, $schedule_name));
			$ground = Doctrine_Core::getTable('DtGround')->find($ground_id);
			if (!$schedule && $ground) {
				$schedule = new DtSchedule();
				$schedule->ground_id = $ground_id;
				$schedule->schedule = $schedule_name;
				$schedule->cost = $cost;
				$schedule->promotion = $promotion;
				$schedule->cost_weekend = $cost_weekend;
				$this->create_success = true;
				$schedule->save();
				
				$q = Doctrine_Query::create()
					->from('DtSchedule s');
				if ($request->hasParameter('ground_id'))
					$q->addWhere('s.ground_id = ?', $request->getParameter('ground_id'));
				$this->schedules = $q->execute();
			}
		}
	} else {
		$q = Doctrine_Query::create()
			->from('DtSchedule s');
		if ($request->hasParameter('ground_id'))
			$q->addWhere('s.ground_id = ?', $request->getParameter('ground_id'))->orderBy('schedule');
		$q = $this->execFilters($q, $form_data);
		$this->schedules = $q->execute();
		return sfView::SUCCESS;
	}
  }


  private function execFilters($q, $form)
  {
	if (!empty($form['ground_id']))
		$q->addWhere('s.ground_id = ?', $form['ground_id']);
	if (!empty($form['schedule']))
		$q->addWhere('s.schedule = ?', $form['schedule']);
	if (!empty($form['cost']))
		$q->addWhere('s.cost = ?', $form['cost']);
	return $q;
  }


  public function executeEdit(sfWebRequest $request)
  {
	if ($request->hasParameter('ground_id') && $request->hasParameter('schedule_name')) {
		$ground_id = $request->getParameter('ground_id');
		$schedule_name = $request->getParameter('schedule_name');
		$schedule = Doctrine_Core::getTable('DtSchedule')->find(array($ground_id, $schedule_name));
		$this->schedule_form = new DtScheduleForm($schedule);
	}
	
	if ($request->hasParameter('schedule')) {
		$form_data = $request->getParameter('schedule');
		$this->schedule_form->bind($form_data);
		$schedule->cost = $form_data['cost'];
		$schedule->cost_weekend = $form_data['cost_weekend'];
		$schedule->promotion = $form_data['promotion'];
		$schedule->save();
		$this->forward('schedule', 'index');
	}
	
	return sfView::SUCCESS;
	
  }
}
